/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.manager;

import com.google.inject.Inject;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import th.dao.AkaunDAO;
import th.model.Akaun;

/**
 *
 * @author fikri
 */
public class AkaunManager {
    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    
    @Inject AkaunDAO akaunDAO;
     
     public String getNoPengenalan(String noAkaun){
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select a.noPengenalanTmbhn from Akaun a " +
                "where akaunTmbhn = :noAkaun");      
        query.setString("noAkaun", noAkaun);
        
        return ( query.uniqueResult() != null ? (String)query.uniqueResult() : "" );
     }
     
     public String getNama(String noAkaunDari) {
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select a.namaAkaunTmbhn from Akaun a " +
                "where akaunTmbhn = :noAkaun");      
        query.setString("noAkaun", noAkaunDari);
        
        return ( query.uniqueResult() != null ? (String)query.uniqueResult() : "" );
        
       
    }
    
     public String getNoAkaun(String noPengenalanTmbhn, String akaunUtama){
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select distinct a.akaunTmbhn from Akaun a " +
                "where noPengenalanTmbhn = :noPengenalanTmbhn and akaunUtama = :akaunUtama");      
        query.setString("noPengenalanTmbhn", noPengenalanTmbhn).setString("akaunUtama", akaunUtama);        
        return ( query.uniqueResult() != null ? (String)query.uniqueResult() : "" );
     }
     
     public Akaun getAkaun (String noAkaun) {
         Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select a from Akaun a " +
                "where a.akaunTmbhn = :akaun");      
        query.setString("akaun", noAkaun);
        return (Akaun) query.uniqueResult();
     }
     
    
     public void deleteAkaun(String noAkaun) {
         Session session = sessionProvider.get();
         StringBuilder sb = new StringBuilder("Select a from Akaun a where a.akaunTmbhn = :akaun");
         Query query = session.createQuery(sb.toString()).setParameter("akaun", noAkaun);
         
         Akaun akaun = (Akaun) query.uniqueResult();
         if (akaun != null) {
             Transaction tx = session.beginTransaction();
             akaunDAO.delete(akaun);
             tx.commit();                
         }         
     }
    
}
